package com.chen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chen.dao.BookMapper;
import com.chen.entity.Book;
import com.chen.service.IBookService;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class BookServiceImpl extends ServiceImpl<BookMapper, Book> implements IBookService {
    @Override
    public List<Book> searchbooks(Book book) {
        LambdaQueryWrapper<Book> queryWrapper = new LambdaQueryWrapper<>();

        if (book.getAuthor() != null && book.getAuthor() != "") {
            queryWrapper.like(Book::getAuthor, book.getAuthor());
        }
        if (book.getName() != null && book.getName() != "") {
            queryWrapper.like(Book::getName, book.getName());
        }
        if (book.getCategory() != null && book.getCategory() != "") {
            queryWrapper.eq(Book::getCategory, book.getCategory());
        }
       return baseMapper.selectList(queryWrapper);
    }

    @Override
    public IPage getPage(Integer pageNum,Book book) {
        IPage page=new Page(pageNum,3);
        LambdaQueryWrapper<Book> queryWrapper = new LambdaQueryWrapper<>();
        if (book.getAuthor() != null && book.getAuthor() != "") {
            queryWrapper.like(Book::getAuthor, book.getAuthor());
        }
        if (book.getName() != null && book.getName() != "") {
            queryWrapper.like(Book::getName, book.getName());
        }
        if (book.getCategory() != null && book.getCategory() != "") {
            queryWrapper.eq(Book::getCategory, book.getCategory());
        }
        IPage page1 = baseMapper.selectPage(page, queryWrapper);
        return page1;
    }
}
